package com.attendance.spring;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.reflect.MethodSignature;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

/**
 * spring aop 工具类
 *
 * @author 鵺丶
 * @date 2021/03/12
 */
public class SpringAopUtils {

    public static Annotation[] getAnnotations(JoinPoint joinPoint)
    {
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();
        if (method == null) {
            return new Annotation[0];
        }
        return method.getDeclaredAnnotations();
    }

    public static <T extends Annotation> T getAnnotation(JoinPoint joinPoint, Class<T> cls){
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();
        return method == null?null:method.getAnnotation(cls);
    }
}
